package com.kaishengit.interceptor;

import java.util.Map;

import com.kaishengit.pojo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ValidateSessionInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;
	
	private String sessionName;

	public void setSessionName(String sessionName) {
		this.sessionName = sessionName;
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		String namespace = invocation.getProxy().getNamespace();
		String actionName = invocation.getProxy().getActionName();
		
		if(("/".equals(namespace) && "index".equals(actionName)) || 
				("/".equals(namespace) && "login".equals(actionName))) {
			return invocation.invoke();
		} else {
			Map<String, Object> session = ActionContext.getContext().getSession();
			User user = (User) session.get(sessionName);
			if(user == null) {
				return "login";
			} else {
				return invocation.invoke();
			}
		}
	}

}
